package com.friend.joey.applicationfriend;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.friend.joey.applicationfriend.Adapters.AlertAdapter;
import com.friend.joey.applicationfriend.ChildActivities.ChildAlertActivity;
import com.friend.joey.applicationfriend.ChildActivities.ChildForgetActivity;
import com.friend.joey.applicationfriend.Classes.AlertMessage;
import com.friend.joey.applicationfriend.Classes.RememRecord;
import com.friend.joey.applicationfriend.Database.MyDatabaseHelper;

import java.util.ArrayList;

/**
 * Created by Joey on 2017/5/29.
 */

public class MainChildActivity extends AppCompatActivity {
    private ImageButton btn_track;
    private ImageButton btn_alert;
    private ImageButton btn_talk;
    private ImageButton btn_forget;
    private ArrayList<AlertMessage> messageList = new ArrayList<>();//备忘消息
    private AlertAdapter adapter;
    private RecyclerView megRecyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chlid);
        btn_track=(ImageButton)findViewById(R.id.button_ctrack);
        btn_alert=(ImageButton)findViewById(R.id.button_calert);
        btn_talk=(ImageButton)findViewById(R.id.button_ctalk);
        btn_forget=(ImageButton)findViewById(R.id.button_cforget);

        btn_track.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.SHOWTRACE");
                startActivity(intent);
            }
        });
        btn_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("com.friend.joey.CHILDFORGET");
                startActivity(intent);
            }
        });
        btn_talk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("com.friend.joey.CHILDTALK");
                startActivity(intent);
            }
        });
        btn_alert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("com.friend.joey.CHILDALERT");
                startActivity(intent);
            }
        });
        initMessages();
        megRecyclerView=(RecyclerView)findViewById(R.id.child_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(MainChildActivity.this);
        megRecyclerView.setLayoutManager(layoutManager);
        adapter = new AlertAdapter(messageList,AlertAdapter.TYPE_INFO);
        adapter.setOnSearchCallback(new AlertAdapter.onAlertCallback() {
            @Override
            public void onDeleteclick(int itempostion) {
                Toast.makeText(MainChildActivity.this,"删除",Toast.LENGTH_SHORT).show();
                deleteFromDatabase(messageList.get(itempostion));
                messageList.remove(itempostion);
                adapter.notifyItemRemoved(itempostion);
                adapter.notifyItemRangeChanged(itempostion,messageList.size()-itempostion);
            }
            @Override
            public void onShowclick(int itempostion) {
                Toast.makeText(MainChildActivity.this,"整条",Toast.LENGTH_SHORT).show();
            }
        });
        megRecyclerView.setAdapter(adapter);

    }

    public void initMessages(){
        AlertMessage rem0;
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(MainChildActivity.this,"TestTalk.db",null,2);//数据库
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("message",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            while(cursor.moveToNext())
            {
                String title= cursor.getString(cursor.getColumnIndex("title"));
                String content=cursor.getString(cursor.getColumnIndex("content"));
                String addtime=cursor.getString(cursor.getColumnIndex("time"));
                int dataType=cursor.getInt(cursor.getColumnIndex("type"));
                int id=cursor.getInt(cursor.getColumnIndex("id"));
                rem0=new AlertMessage(id,dataType,title,content,addtime);
                messageList.add(rem0);
            }
        } else{
            AlertMessage rem1=new AlertMessage(AlertMessage.TYPE_FORGET,"添加备忘","止咳糖浆");
            messageList.add(rem1);
            AlertMessage rem2=new AlertMessage(AlertMessage.TYPE_LOCATION,"出门","京剧大赏");
            messageList.add(rem2);
            AlertMessage rem3=new AlertMessage(AlertMessage.TYPE_MAP,"查看地图","止咳糖浆3");
            messageList.add(rem3);
        }
    }
    private void deleteFromDatabase(AlertMessage rem){
        //String s=rem.getTitle()+rem.getContent()+Integer.toString(rem.getId());
        //Toast.makeText(ChildForgetActivity.this, "删除"+s, Toast.LENGTH_SHORT).show();
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(MainChildActivity.this,"TestTalk.db",null,2);//数据库
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int id=rem.getId();
        db.delete("message","id=?",new String[]{Integer.toString(id)});
        db.close();
    }
}
